from ninja import Schema
from typing import Optional, List
"""
    没有Pydantic：
            需要手动定义每个字段及其类型
            需要手动验证
            # 需要手动验证所有字段
        def login(data):
            if 'username' not in data:
                return "错误：缺少用户名"
            if 'password' not in data:  
                return "错误：缺少密码"
            if not isinstance(data['username'], str):
                return "错误：用户名必须是字符串"
    # ... 更多验证代码
    有Pydantic：
            可以自动生成字段及其类型
            可以自动生成文档
            可以自动生成验证器
            可以自动生成类型提示
            可以自动生成JSON模式
"""
class RegisterIn(Schema):
    username: str
    password: str
    email: Optional[str] = None

class RegisterOut(Schema):
    message: str
    username: str

class LoginIn(Schema):
    username: str
    password: str

class LoginOut(Schema):
    token: str
    username: str
    user_id: int
    expiry: int

class ChatIn(Schema):
    session_id: str = "default_session"
    user_input: str
    mode: str | None = None  # "shallow" | "deep"
    file_content: Optional[str] = None  # 文件内容（从文件提取的文本）

class ChatOut(Schema):
    reply: str
    timestamp: str
    retrieval_stats: Optional[dict] = None

class HistoryOut(Schema):
    history: str

class ErrorResponse(Schema):
    error: str

class RegenerateIn(Schema):
    session_id: str = "default_session"
    mode: str | None = None  # "shallow" | "deep"

class RegenerateOut(Schema):
    reply: str
    timestamp: str

# 用户管理相关schemas
class UserInfo(Schema):
    id: int
    username: str
    email: Optional[str] = None
    created_at: str
    updated_at: str
    is_active: bool
    last_login: Optional[str] = None

class UserListOut(Schema):
    users: list[UserInfo]
    total: int
    page: int
    page_size: int

class UserUpdateIn(Schema):
    username: Optional[str] = None
    email: Optional[str] = None
    is_active: Optional[bool] = None

class UserUpdateOut(Schema):
    message: str
    user: UserInfo

class UserDeleteOut(Schema):
    message: str

# 对话历史相关schemas
class ChatHistoryItem(Schema):
    session_id: str
    user_id: int
    username: str
    content: str
    timestamp: str

class ChatHistoryOut(Schema):
    conversations: list[ChatHistoryItem]
    total: int
    page: int
    page_size: int

class ChatHistoryFilter(Schema):
    user_id: Optional[int] = None
    username: Optional[str] = None
    start_date: Optional[str] = None
    end_date: Optional[str] = None
    page: int = 1
    page_size: int = 20

# 数据分析相关schemas
class AnalyticsDataOut(Schema):
    """数据分析输出模型"""
    total_users: int
    total_conversations: int
    active_users: int
    total_messages: int
    user_growth: List[dict]  # 用户增长数据
    conversation_trend: List[dict]  # 对话趋势数据
    top_users: List[dict]  # 活跃用户排行
    avg_messages_per_session: float
    hourly_activity: List[dict]  # 24小时活跃度分布
    fault_distribution: List[dict]  # 故障类型分布
    resolution_trend: List[dict]  # 问题解决率趋势

# 用户反馈相关schemas
class FeedbackIn(Schema):
    """用户反馈输入模型"""
    session_id: str
    is_resolved: bool  # True=问题已解决, False=问题未解决

class FeedbackOut(Schema):
    """用户反馈输出模型"""
    message: str
    session_id: str
    is_resolved: bool

# 会话相关schemas
class SessionInfo(Schema):
    """会话信息模型"""
    session_id: str
    session_name: Optional[str] = None
    created_at: str
    updated_at: str

class SessionListOut(Schema):
    """会话列表输出模型"""
    sessions: List[SessionInfo]
